import java.io.*;
import java.util.ArrayList;
public class fdg1 {
    public static void main(String[]args) throws IOException {
        String str1,str2;
        int age1,age2;
        BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("欢迎使用住户信息管理系统");
        System.out.println("请进行身份验证");
        str1=buf.readLine();
        age1 =Integer.parseInt(str1);
        if(age1==1111){
            //管理员
            while(true){
                System.out.println("欢迎使用住户信息管理系统(管理员)");
                System.out.println("1，添加住户");
                System.out.println("2，删除住户");
                System.out.println("3，修改住户");
                System.out.println("4，查看所有住户");
                System.out.println("5，退出系统");
                System.out.println("请输入编号来使用系统");
                str2=buf.readLine();
                age2 =Integer.parseInt(str2);
                switch (str2){
                    case"1":
                        addstudent();
                        break;
                    case"2":
                        deletestudent();
                        break;
                    case"3":
                        updatestudent();
                        break;
                    case"4":
                        bianlistudent();
                        break;
                    case"5":
                        System.out.println("谢谢使用");
                        System.exit(0);
                    default:
                        System.out.println("输入错误");
                        break;
                }if (age2==5){
                    break;
                }
            }

        }else{
            while(true){
                System.out.println("欢迎使用住户信息管理系统(访客)");
                System.out.println("1，查看所有住户");
                System.out.println("2，退出系统");
                System.out.println("请输入编号来使用系统");
                str2=buf.readLine();
                age2 =Integer.parseInt(str2);
                switch (str2) {
                    case "1":
                        bianlistudent();
                        break;
                    case "2":
                        System.out.println("谢谢使用");
                        System.exit(0);
                    default:
                        System.out.println("输入错误");
                        break;
                }if (age2==5){
                    break;
                }
            }
        }
    }
    //添加
    public  static  void  addstudent () throws IOException {
        String sid,name,sge,address;
        int age;
        BufferedReader buf =new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            System.out.println("请输入门号");
            sid=buf.readLine();
            if(sid.length()==4){
                break;
            }else { System.out.println("门号格式不对，请输入正确格式");}
        }
        String str;
        System.out.println("请输入户主姓名");
        name=buf.readLine();
        System.out.println("请输入户主年龄");
        sge=buf.readLine();
        System.out.println("请输入所在位置");
        address=buf.readLine();
        age=Integer.parseInt(sge);
        String str233;
        boolean a=true;
        str233="门号:"+sid+",户主姓名:"+name+",户主年龄:"+age+",所在位置:"+address+"\n";
        BufferedWriter buf233=new BufferedWriter(new FileWriter("warehouse.txt",a));
        BufferedReader in =new BufferedReader(new FileReader("warehouse.txt"));
        while(( str=in.readLine())!=null){
            String syyy=str.substring(3,7);
            if(sid.equals(syyy)){
                System.out.println("门号重复，请重新输入");
                return;
            }
            else{}}
            in.close();
            buf233.write(str233);
        buf233.close();
    }
    //删除
    public  static  void  deletestudent () throws IOException {
        String sid;
        BufferedReader buf =new BufferedReader(new InputStreamReader(System.in));
        System.out.println("请输入门号");
        sid=buf.readLine();
        String str;
        try(
                BufferedReader in =new BufferedReader(new FileReader("warehouse.txt"));
                BufferedWriter out =new BufferedWriter(new FileWriter("warehouse2.txt"));
        ){
            while((str=in.readLine())!=null){
                String syyy=str.substring(3,7);
                if(sid.equals(syyy)){
                    str="门号:"+syyy;
                    out.write(str);
                    out.newLine();
                    System.out.println("删除成功");
                }else{
                    out.write(str);
                    out.newLine();
                }
            }
            out.flush();
            in.close();
            out.close();
            File fi1 =new File("warehouse.txt");
            fi1.delete();
            File fi2=new File("warehouse2.txt");
            fi2.renameTo(new File("warehouse.txt"));
//            long pos=1l;
//            RandomAccessFile rad=new RandomAccessFile("warehouse.txt", "r");
//            rad.seek(pos);
        }

        catch(IOException ioe){
        }

    }
    //修改
    public  static  void  updatestudent() throws IOException {
        String str;
        BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("请输入门号");
        str=buf.readLine();
        String str6;
        try(
                BufferedReader in =new BufferedReader(new FileReader("warehouse.txt"));
                BufferedWriter out =new BufferedWriter(new FileWriter("warehouse2.txt"));
        ){
            while((str6=in.readLine())!=null){
                String syyy=str6.substring(3,7);
                if(str.equals(syyy)){
                    BufferedReader buf12 =new BufferedReader(new InputStreamReader(System.in));
                    System.out.println("请输入户主姓名");
                    String name1=buf12.readLine();
                    System.out.println("请输入户主年龄");
                    String sge1=buf12.readLine();
                    System.out.println("请输入所在位置");
                    String addres1=buf12.readLine();
                    int age1;
                    age1=Integer.parseInt(sge1);
                    str6="门号:"+str+",户主姓名:"+name1+",户主年龄:"+age1+",所在位置:"+addres1;
                    out.write(str6);
                    System.out.println("修改成功");
                    out.newLine();
                }else{
                    out.write(str6);
                    out.newLine();
                }
            }
            out.flush();
            in.close();
            out.close();
            File fi1 =new File("warehouse.txt");
            fi1.delete();
            File fi2=new File("warehouse2.txt");
            fi2.renameTo(new File("warehouse.txt"));
        }

        catch(IOException ioe){
        }
    }
    //查看所有学生
    public  static  void  bianlistudent() throws IOException {
        BufferedReader buf223 =new BufferedReader(new FileReader("warehouse.txt"));
        String str233;
        while ((str233=buf223.readLine())!=null){
            System.out.println(str233);
        }
        buf223.close();
    }
}
